<extend name="./Application/Admin/View/Layouts/application.html"/>

<block name="content">
    <div class="admin-content">

        <div class="am-cf am-padding">
            <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">编辑用户组</strong> /
                <small>Edit Group</small>
            </div>
        </div>

        <div class="am-g">
            <div class="am-u-sm-12 am-u-md-12">
                <div class="am-tab-panel">

                    <!--<form class="am-form" action="{{:U('edit_group',['id'=>$group[id]])}}" method="post">-->

                    <!--ajax提交,id要通过post方式传到后台-->
                    <form class="am-form">
                        <input type="hidden" name="id" value="{{$group.id}}">

                        <div class="am-g am-margin-top">
                            <div class="am-u-sm-4 am-u-md-2 am-text-right">
                                用户组名
                            </div>
                            <div class="am-u-sm-8 am-u-md-4 am-u-end col-end">
                                <input type="text" class="am-input-sm" name="title" value="{{$group.title}}">
                            </div>
                        </div>

                        <div class="am-g am-margin-top">
                            <div class="am-u-sm-4 am-u-md-2 am-text-right">
                                权限列表
                            </div>

                            <div class="am-u-sm-8 am-u-md-8 am-u-end col-end">


                                <volist name="menus" id="menu">
                                    <div class="am-panel am-panel-default">
                                        <div class="am-panel-hd">
                                            <label class="am-checkbox-inline">
                                                <input type="checkbox" value="{{$menu.id}}" name="rules[]" class="rule_one"
                                                <if condition="in_array($menu['id'],explode(', ',$group[rules]))">checked</if> >
                                                <span class="am-badge am-badge-secondary am-text-sm am-radius">{{$menu.title}}</span>
                                            </label>
                                        </div>
                                        <div class="am-panel-bd">

                                            <volist name="menu.children" id="children">
                                                <div class="am-g am-margin-top">
                                                    <div class="am-u-sm-4 am-u-md-3 am-text-right p2">
                                                        <label class="am-checkbox-inline">
                                                            <input type="checkbox" value="{{$children.id}}" name="rules[]" class="rule_two"
                                                            <if condition="in_array($children['id'],explode(', ',$group['rules']))">checked</if>>
                                                            <span class="am-badge am-badge-success am-radius">{{$children.title}}</span>
                                                        </label>
                                                    </div>
                                                    <div class="am-u-sm-8 am-u-md-9 am-u-end col-end p3">

                                                        <volist name="children.child" id="value">
                                                            <label class="am-checkbox-inline">
                                                                <input type="checkbox" value="{{$value.id}}" name="rules[]" class="rule_three"
                                                                <if condition="in_array($value['id'],explode(', ',$group['rules']))">checked</if>>{{$value.title}}
                                                            </label>
                                                        </volist>

                                                    </div>
                                                </div>
                                            </volist>

                                        </div>
                                    </div>
                                </volist>

                            </div>
                        </div>

                        <div class="am-margin">
                            <button type="submit" class="submit am-btn am-btn-primary">保存修改</button>
                        </div>

                    </form>
                </div>
            </div>
        </div>
    </div>
</block>

<block name="js">
    <script>
        $(function () {
            //一级选中，二级三级全选中
            $('.rule_one').click(function () {
                var checked = $(this).prop('checked');
                $(this).parents('.am-panel-hd').next().find('input').prop('checked', checked);
            })
            //二级选中，三级全选中
            $('.rule_two').click(function () {
                var checked = $(this).prop('checked');
                $(this).parents('.p2').next().find('input').prop('checked', checked);

                //二级选中一个，一级也选中
                var checked_length = $('.rule_two:checked').length;
                if (checked_length) {
                    $(this).parents('.am-panel').find('.rule_one').prop('checked', true);
                } else {
                    $(this).parents('.am-panel').find('.rule_one').prop('checked', false);
                }
            });
            //三级选中一个，二级一级也选中
            $('.rule_three').click(function () {
                var checked_length = $('.rule_three:checked').length;
                if (checked_length) {
                    $(this).parents('.am-panel').find('.rule_one').prop('checked', true);
                    $(this).parents('.p3').prev().find('.rule_two').prop('checked', true);
                } else {
                    $(this).parents('.am-panel').find('.rule_one').prop('checked', false);
                    $(this).parents('.p3').prev().find('.rule_two').prop('checked', false);
                }
            });


            //ajax编辑用户组
            $('.submit').click(function () {
                var info = $('input').serialize();
                $.ajax({
                    type:'POST',
                    url:"{{:U('edit_group')}}",
                    data:info,
                    success:function (info) {
                        if(info.status == 1){
                            alert(info.msg);
                            location.href = "{{:U('group_manage')}}";
                        }else{
                            alert(info.msg);
                            return false;
                        }
                    }
                });
                return false;
            })
        })
    </script>
</block>